<template>
  <div class="profit">
    <!-- 我的矿机 -->
    <!-- <div class="box mb-15">
      <div class="flex flex-rc">
        <div class="title">{{ $t('我的矿机') }}</div>
      </div>

      <van-button
        style="height: 45px"
        type="primary"
        block
        round
        @click="router.push('/miner')"
      >
        <div class="fz-15 cl-black fw-bold">{{ $t('激活矿机') }}</div>
      </van-button>
    </div> -->

    <!-- 收益提取 -->
    <div class="box mb-15">
      <div class="flex flex-rc">
        <div class="title">{{ $t('收益提取') }}</div>
      </div>

      <div v-if="records1.length">
        <ul class="list">
          <li
            class="list__item container bcl-mask radius mb-15"
            v-for="(v, i) in records1"
            :key="i"
          >
            <div class="flex flex-center mb-15">
              <img
                class="mr-5"
                style="width: 15px"
                src="../assets/img/profit/profit.png"
              />
              <span>{{ $t('总资产') }}：</span>
              <span>{{ v.amount?.toFixed(6) || 0 }} C</span>
            </div>

            <div class="flex flex-rc">
              <!-- <div class="ta-center">
                <div class="mb-15">
                  <div>{{ $t('激活矿机收益') }}</div>
                  <div class="fz-20">{{ v.profit_pa?.toFixed(6) || 0 }} U</div>
                </div>

                <van-button
                  style="width: 100px"
                  type="primary"
                  @click="addWithdraw(v.id, 0)"
                >
                  <span class="cl-black">{{ $t('提取') }}</span>
                </van-button>
              </div> -->

              <div class="ta-center">
                <div class="mb-15">
                  <div>{{ $t('提现收益') }}</div>
                  <div class="fz-20">{{ v.profit_pb?.toFixed(6) || 0 }} C</div>
                </div>

                <van-button
                  style="width: 100px"
                  type="primary"
                  @click="addWithdraw(v.id, 1)"
                >
                  <span class="cl-black">{{ $t('提取') }}</span>
                </van-button>
              </div>
            </div>
          </li>
        </ul>

        <!-- 分页 -->
        <van-pagination
          mode="simple"
          :prev-text="$t('上一页')"
          :next-text="$t('下一页')"
          :total-items="total1"
          :items-per-page="size1"
          @change="getRecords1"
          v-model="page1"
        ></van-pagination>
      </div>

      <van-empty :description="$t('暂无数据')" v-else></van-empty>
    </div>

    <!-- 收益详情 -->
    <div class="box mb-20">
      <div class="flex flex-rc">
        <div class="title">{{ $t('收益详情') }}</div>
      </div>

      <tabs-comp
        :tabs="tabs"
        @change=";(page2 = 1), getRecords2()"
        v-model:tabIndex="tabIndex"
      ></tabs-comp>

      <div v-if="records2.length">
        <!-- 用户理财 -->
        <ul class="list list--50" v-if="tabIndex === 0">
          <li
            class="list__item container bcl-mask mb-15"
            v-for="(v, i) in records2"
            :key="i"
          >
            <div class="item">
              <span class="label">{{ $t('总认购收益额') }}</span>
              <span class="value">{{ v.amount_all }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('获取收益倍数') }}</span>
              <span class="value">{{ v.amount_times }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('退场额') }}</span>
              <span class="value">{{ v.amount_exit }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('日化收益率') }}(%)</span>
              <span class="value">{{ (v.rate_year / 365).toFixed(2) }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('阶段收益') }}</span>
              <span class="value">{{ v.pr }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('期次') }}</span>
              <span class="value">{{ v.batch }}</span>
            </div>
            <div class="item" style="width: 100%">
              <span class="label">{{ $t('更新时间') }}</span>
              <span class="value">{{ formatDateTime(v.update_time) }}</span>
            </div>
          </li>
        </ul>

        <!-- 客户分红 -->
        <ul class="list list--50" v-else-if="tabIndex === 1">
          <li
            class="list__item container bcl-mask mb-15"
            v-for="(v, i) in records2"
            :key="i"
          >
            <div class="item" style="width: 100%">
              <span class="label">{{ $t('有效客户数') }}</span>
              <span class="value">{{ v.cnt_sons }}</span>
            </div>
            <div class="item" style="width: 100%">
              <span class="label">{{ $t('有效客户认购收益总额') }}</span>
              <span class="value">{{ v.total_amount }}</span>
            </div>
            <div class="item" style="width: 100%">
              <span class="label">{{ $t('客户当期理财收益总额') }}</span>
              <span class="value">{{ v.total_profit }}</span>
            </div>
            <div class="item" style="width: 100%">
              <span class="label">{{ $t('获得客户比例') }}(%)</span>
              <span class="value">{{ v.rate }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('分红') }}</span>
              <span class="value">{{ v.bonus }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('期次') }}</span>
              <span class="value">{{ v.batch }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('更新时间') }}</span>
              <span class="value">{{ formatDateTime(v.update_time) }}</span>
            </div>
          </li>
        </ul>

        <!-- 团队奖励 -->
        <ul class="list list--50" v-else>
          <li
            class="list__item container bcl-mask mb-15"
            v-for="(v, i) in records2"
            :key="i"
          >
            <div class="item">
              <span class="label">{{ $t('有效客户数') }}</span>
              <span class="value">{{ v.cnt_sons }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('等级') }}</span>
              <span class="value">{{ v.level }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('伞下总业绩') }}</span>
              <span class="value">{{ v.total_all }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('小市场总业绩') }}</span>
              <span class="value">{{ v.total_small }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('大市场达到等级') }}</span>
              <span class="value">{{ v.big_level }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('大市场总业绩') }}</span>
              <span class="value">{{ v.total_big }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('获得团队比例') }}(%)</span>
              <span class="value">{{ v.rate }}</span>
            </div>
            <div class="item" style="width: 100%">
              <span class="label">{{ $t('团队理财收益总额') }}</span>
              <span class="value">{{ v.total_profit }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('奖励') }}</span>
              <span class="value">{{ v.rewards }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('期次') }}</span>
              <span class="value">{{ v.batch }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('更新时间') }}</span>
              <span class="value">{{ formatDateTime(v.update_time) }}</span>
            </div>
          </li>
        </ul>

        <!-- 分页 -->
        <van-pagination
          mode="simple"
          :prev-text="$t('上一页')"
          :next-text="$t('下一页')"
          :total-items="total2"
          :items-per-page="size2"
          @change="getRecords2"
          v-model="page2"
        ></van-pagination>
      </div>

      <van-empty :description="$t('暂无数据')" v-else></van-empty>
    </div>

    <!-- 提现单 -->
    <div class="box">
      <div class="flex flex-rc">
        <div class="title">{{ $t('提现单') }}</div>
      </div>

      <div v-if="records3.length">
        <ul class="list">
          <li
            class="list__item container bcl-mask mb-15"
            v-for="(v, i) in records3"
            :key="i"
          >
            <div class="item">
              <span class="label">{{ $t('提现金额') }}</span>
              <span class="value">{{ v.amount }}</span>
            </div>
            <div class="item">
              <span class="label">{{ $t('提现时间') }}</span>
              <span class="value">{{ formatDateTime(v.create_time) }}</span>
            </div>
            <div class="item" v-if="v.hash">
              <span class="label">{{ $t('哈希') }}</span>
              <span class="value click" @click="copyString(v.hash)">
                {{ v.hash }}
              </span>
            </div>
          </li>
        </ul>

        <!-- 分页 -->
        <van-pagination
          mode="simple"
          :prev-text="$t('上一页')"
          :next-text="$t('下一页')"
          :total-items="total3"
          :items-per-page="size3"
          @change="getRecords3"
          v-model="page3"
        ></van-pagination>
      </div>

      <van-empty :description="$t('暂无数据')" v-else></van-empty>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { showToast, formatDateTime, copyString } from 'js/utils'
import apis from 'js/apis'

import tabsComp from './tabs.vue'

const router = useRouter()

const tabs = ref([$t('用户理财'), $t('客户分红'), $t('团队奖励')])
const tabIndex = ref(0)

// 收益提取
const records1 = ref([])
const page1 = ref(1)
const size1 = ref(2)
const total1 = ref(0)

// 收益详情
const records2 = ref([])
const page2 = ref(1)
const size2 = ref(5)
const total2 = ref(0)

// 提现单
const records3 = ref([])
const page3 = ref(1)
const size3 = ref(5)
const total3 = ref(0)

getRecords1()
getRecords2()
getRecords3()

// 获取收益提取
async function getRecords1() {
  const params = {
    page: page1.value,
    limit: size1.value,
  }

  const { code, data } = await apis.pledge.getDetails(params)

  if (code === 1) {
    const records = data.data || []

    records.forEach((v) => {
      v.amount = (v.profit_pa || 0) + (v.profit_pb || 0)
    })

    records1.value = records
    total1.value = data.count || 0
  }
}

// 获取收益详情
async function getRecords2() {
  const params = {
    page: page2.value,
    limit: size2.value,
  }

  const { code, data } =
    tabIndex.value === 0
      ? await apis.profit.getProfits(params)
      : tabIndex.value === 1
      ? await apis.profit.getAgents(params)
      : await apis.profit.getTeams(params)

  if (code === 1) {
    records2.value = data.data || []
    total2.value = data.count || 0
  }
}

// 获取提现记录
async function getRecords3() {
  const params = {
    page: page3.value,
    limit: size3.value,
  }

  const { code, data } = await apis.profit.getWithdraws(params)

  if (code === 1) {
    records3.value = data.data || []
    total3.value = data.count || 0
  }
}

// 提现
async function addWithdraw(id = 0, mode = 0) {
  const params = {
    id,
    mode,
  }

  const { code } = await apis.profit.addWithdraw(params)

  if (code === 1) {
    showToast($t('操作成功'))

    setTimeout(() => {
      page1.value = 1
      page3.value = 1
      getRecords1()
      getRecords3()
    }, 2000)
  }
}
</script>

<style lang="scss" scoped></style>
